package com.ruoyi.fantasy.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.huanzhidadi.fantasy.common.constant.RedisConstants;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.domain.FantasyTaskRewardBO;
import com.ruoyi.fantasy.mapper.FantasyItemtypeMapper;
import com.ruoyi.fantasy.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyRewardMapper;
import com.huanzhidadi.fantasy.bean.domain.FantasyReward;
import com.ruoyi.fantasy.service.IFantasyRewardService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 奖励 服务层实现
 * 
 * @author wanghui
 * @date 2019-07-05
 */
@Service
public class FantasyRewardServiceImpl implements IFantasyRewardService 
{
	@Autowired
	private FantasyRewardMapper fantasyRewardMapper;

	@Resource
	private FantasyItemtypeMapper fantasyItemtypeMapper;

	@Autowired
	private RedisUtil redisUtil;

	/**
     * 查询奖励信息
     * 
     * @param id 奖励ID
     * @return 奖励信息
     */
    @Override
	public FantasyReward selectFantasyRewardById(String id)
	{
	    return fantasyRewardMapper.selectFantasyRewardById(id);
	}
	
	/**
     * 查询奖励列表
     * 
     * @param fantasyReward 奖励信息
     * @return 奖励集合
     */
	@Override
	public List<FantasyReward> selectFantasyRewardList(FantasyReward fantasyReward)
	{
	    return fantasyRewardMapper.selectFantasyRewardList(fantasyReward);
	}
	
    /**
     * 新增奖励
     * 
     * @param fantasyReward 奖励信息
     * @return 结果
     */
	@Override
	public int insertFantasyReward(FantasyReward fantasyReward)
	{
	    return fantasyRewardMapper.insertFantasyReward(fantasyReward);
	}
	
	/**
     * 修改奖励
     * 
     * @param fantasyReward 奖励信息
     * @return 结果
     */
	@Override
	public int updateFantasyReward(FantasyReward fantasyReward)
	{
	    return fantasyRewardMapper.updateFantasyReward(fantasyReward);
	}

	/**
     * 删除奖励对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyRewardByIds(String ids)
	{
		return fantasyRewardMapper.deleteFantasyRewardByIds(Convert.toStrArray(ids));
	}

	@Override
	public void transforData(List<FantasyReward> list) {
		Map<String, String> itemTypes = redisUtil.hmGet(RedisConstants.FANTASY_ITEM_TYPE);
		for (FantasyReward fantasyReward : list) {
			if (fantasyReward.getType() == 4) {
				FantasyItemType fantasyItemType = JSONObject.parseObject(itemTypes.get(fantasyReward.getValue()), FantasyItemType.class);
				if (null == fantasyItemType) {
					fantasyItemType = fantasyItemtypeMapper.selectFantasyItemtypeById(fantasyReward.getValue());
				}
				fantasyReward.setValue(fantasyItemType.getName());
			}
		}
	}

}
